package com.itholmes.tomcat;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.itholmes.ServletConfig.ServletConfig;
import com.itholmes.ServletConfig.ServletConfigImpl;
import com.itholmes.ServletContext.ServletContext;
import com.itholmes.ServletContext.ServletContextImpl;
import com.itholmes.bean.ServletUrlAndClassAndInitparam;

public class MyMapping {
	
	
	//映射/url和类路径
	public ServletUrlAndClassAndInitparam getMappingAndClassURL() {
		
		ServletUrlAndClassAndInitparam urlAndClassAndInitparam = new ServletUrlAndClassAndInitparam();
		
		Map<String, String> url_class = new HashMap<String, String>();
		Map<String, String> servlet = new HashMap<String, String>();
		Map<String, String> servlet_mapping = new HashMap<String, String>();
		
		Map<String, String> config = new HashMap<String, String>();
		
		try {
			SAXReader reader = new SAXReader();
			Document document = reader.read(new File("src/com/itholmes/tomcat/web.xml"));
			Element rootElement = document.getRootElement();
			//遍历web-app目录
			Iterator<Element> elementIterator = rootElement.elementIterator();
			while(elementIterator.hasNext()) {
				
				Element element = elementIterator.next();
				
				if((element.getName()).equalsIgnoreCase("servlet")) {
					//遍历servlet标签
					Iterator<Element> servlet_element = element.elementIterator();
					String servlet_name = "";
					String servlet_class = "";
					while(servlet_element.hasNext()) {
						Element next = servlet_element.next();
						if((next.getName()).equalsIgnoreCase("servlet-name")) {
							servlet_name = next.getStringValue();
						}else if((next.getName()).equalsIgnoreCase("servlet-class")) {
							servlet_class = next.getStringValue();
						}else if((next.getName()).equalsIgnoreCase("init-param")) {
							Iterator<Element> init_param = next.elementIterator();
							String key = "";
							String value = "";
							while(init_param.hasNext()) {
								Element param = init_param.next();
								if((param.getName()).equalsIgnoreCase("param-name")) {
									key = param.getStringValue();
								}else if((param.getName()).equalsIgnoreCase("param-value")) {
									value = param.getStringValue();
								}
							}
							config.put(key, value);
						}
					}
					servlet.put(servlet_name, servlet_class);
				}else if((element.getName()).equalsIgnoreCase("servlet-mapping")) {
					//遍历servlet-mapping标签
					Iterator<Element> servlet_element = element.elementIterator();
					String servlet_name = "";
					String url_pattern = "";
					while(servlet_element.hasNext()) {
						Element next = servlet_element.next();
						if((next.getName()).equalsIgnoreCase("servlet-name")) {
							servlet_name = next.getStringValue();
						}else if((next.getName()).equalsIgnoreCase("url-pattern")) {
							url_pattern = next.getStringValue();
						}
						servlet_mapping.put(servlet_name, url_pattern);
					}
					
				//遍历获取init-param
				}
			}
			
			//遍历两个map，得到最终url路径对应class类路径
			Set<Entry<String,String>> entrySet = servlet_mapping.entrySet();
			Iterator<Entry<String, String>> iterator = entrySet.iterator();
			while(iterator.hasNext()) {
				Entry<String, String> next = iterator.next();
				//servlet的值 和 拿到servlet_mapping的value值
				url_class.put(next.getValue(), servlet.get(next.getKey()));
			}
			
			
			ServletConfigImpl servletConfig = new ServletConfigImpl();
			servletConfig.setConfig_attribute(config);
			
			urlAndClassAndInitparam.setConfig(servletConfig);
			urlAndClassAndInitparam.setUrl_class(url_class);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException("Mapping映射错误！");
		}
		return urlAndClassAndInitparam;
	}
	
	
	
	public ServletContext getServletContext() {
		ServletContext context = new ServletContextImpl();
		try {
			SAXReader reader = new SAXReader();
			Document document = reader.read(new File("src/com/itholmes/tomcat/web.xml"));
			Element rootElement = document.getRootElement();
			//遍历web-app目录
			Iterator<Element> elementIterator = rootElement.elementIterator();
			while(elementIterator.hasNext()) {
				Element element = elementIterator.next();
				if((element.getName()).equalsIgnoreCase("context-param")) {
					Iterator<Element> context_param = element.elementIterator();
					String key = "";
					Object value = null;
					while(context_param.hasNext()) {
						Element next = context_param.next();
						if((next.getName()).equalsIgnoreCase("param-name")) {
							key = next.getStringValue();
						}else if((next.getName()).equalsIgnoreCase("param-value")) {
							value = next.getStringValue();
						}else {
							throw new RuntimeException("web.xml异常");
						}
					}
					context.setAttribute(key, value);
				}
			}
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return context;
	} 
	
}
